package com.hanter.demo.raddemo.test

import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.hanter.android.radui.mvp.MvpTitleBarActivity
import com.hanter.android.radwidget.BannerView
import com.hanter.android.radwidget.viewpager.ViewPager
import com.hanter.demo.raddemo.R
import kotlinx.android.synthetic.main.activity_test_banner.*

class TestBannerActivity :
    MvpTitleBarActivity<TestBannerContract.View, TestBannerContract.Presenter>(),
    TestBannerContract.View {

    private lateinit var imageUrls: ArrayList<ImageInfo>

    override fun getLayout(): Int = R.layout.activity_test_banner

    override fun createPresenter() = TestBannerPresenter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.imageUrls = ArrayList()

        this.imageUrls.add(
            ImageInfo(
                1920,
                768,
                "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1573231045958&di=08777f2beb345a39899e609096842a91&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201312%2F27%2F20131227232853_fJCmr.jpeg"
            )
        )

        this.imageUrls.add(
            ImageInfo(
                800,
                579,
                "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1573231056826&di=22388264ffd2c76b4b3068b2b4c653e8&imgtype=0&src=http%3A%2F%2Fpic2.52pk.com%2Ffiles%2F120807%2F1283568_102511_4.jpg"
            )
        )

        this.imageUrls.add(
            ImageInfo(
                1366,
                768,
                "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1573231045958&di=08777f2beb345a39899e609096842a91&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201312%2F27%2F20131227232853_fJCmr.jpeg"
            )
        )

        this.imageUrls.add(
            ImageInfo(
                828,
                1793,
                "http://panxipublic.oss-cn-hangzhou.aliyuncs.com/photos/86c0874bdb19ddc472149f6b86f68bec.jpg"
            )
        )

        this.imageUrls.add(
            ImageInfo(
                834,
                626,
                "http://panxipublic.oss-cn-hangzhou.aliyuncs.com/photos/4b2aee8616f27587e32a9cda322777cc.jpg"
            )
        )

        this.imageUrls.add(
            ImageInfo(
                826,
                1102,
                "http://panxipublic.oss-cn-hangzhou.aliyuncs.com/photos/60214e2bc39bfa31ef375406ce9cd30b.jpg"
            )
        )
        initViews()
//        updateBannerView()
    }

    private fun initViews() {
        val images = ArrayList<String>()
        val titles = ArrayList<String>()
        this.imageUrls.forEachIndexed({ index: Int, imageInfo: ImageInfo ->
            images.add(imageInfo.img)
            titles.add("$index")
        })

//        images.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1573231045958&di=08777f2beb345a39899e609096842a91&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201312%2F27%2F20131227232853_fJCmr.jpeg")
//        images.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1573231056826&di=22388264ffd2c76b4b3068b2b4c653e8&imgtype=0&src=http%3A%2F%2Fpic2.52pk.com%2Ffiles%2F120807%2F1283568_102511_4.jpg")
//        images.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1573231060174&di=bc3c96b2c21867cbd84b8f32f9127651&imgtype=0&src=http%3A%2F%2Fimg3.duitang.com%2Fuploads%2Fitem%2F201503%2F05%2F20150305213902_TP2ja.jpeg")
//        images.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1573231067360&di=6db2392eaa536655c69d341cc97aa38f&imgtype=0&src=http%3A%2F%2Fpic15.nipic.com%2F20110617%2F6901455_014602752117_2.jpg")
//        images.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1573231112745&di=1ac1ff4ee3c7ce2ce8dc609005a3bd83&imgtype=0&src=http%3A%2F%2Fdmimg.5054399.com%2Fallimg%2Foptuji%2Fcl3%2F040.jpg")
//
//        titles.add("1")
//        titles.add("2")
//        titles.add("3")
//        titles.add("4")
//        titles.add("5")

        bvImages.setBannerLoader(MomentImageLoader())
            .setIndicatorStyle(BannerView.INDICATOR_CIRCLE)
            .setImages(images)
            .setBannerTitles(titles)
            .start()

        bvImages.setOnItemClickListener(object : BannerView.OnItemClickListener {
            override fun onItemClick(bannerView: BannerView?, position: Int) {

            }
        })

        bvImages.setOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                updatePager(position, positionOffset)
            }

            override fun onPageSelected(position: Int) {

            }
        })
    }

    private fun updateBannerView() {
//        bvImages.stopAutoPlay()

        val images = ArrayList<String>()
        val titles = ArrayList<String>()
        this.imageUrls.forEach { list ->
            images.add(list.img)
            titles.add("")
        }
        bvImages.update(images, titles)

        bvImages.start()

//        bvImages.viewPager.setPageTransformer(false, null)

        updatePager(0, 0f)
    }

    private fun updatePager(position: Int, positionOffset: Float) {
        Log.e("Test", "position: ${position}, offset: ${positionOffset}")

        this.imageUrls.let { urls ->
            if (urls.isEmpty()) {
                return
            }

            val imageInfo = urls.getOrNull(position) ?: return
            val height = ScreenUtil.getScreenWidth(this) * imageInfo.h / imageInfo.w
            val nextPos = (position + 1) % urls.size
            val layoutHeight = urls.getOrNull(nextPos)?.let { imageInfo2 ->
                val height2 = ScreenUtil.getScreenWidth(this) * imageInfo2.h / imageInfo2.w
                height * (1 - positionOffset) + height2 * positionOffset
            } ?: let {
                height * 1f
            }

            val bvParams = bvImages.layoutParams
            bvParams.height = (layoutHeight + 0.5f).toInt()
            bvImages.layoutParams = bvParams
        }
    }

    class MomentImageLoader : BannerView.BannerLoader {

        private val opts = RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
            .dontAnimate()
            .dontTransform()
//            .placeholder(R.mipmap.ic_launcher)

        override fun displayImage(
            context: Context,
            path: Any?,
            placeHolder: Drawable?,
            imageView: ImageView
        ) {

            Glide.with(imageView)
                .load(path)
                .apply(opts)
                .into(imageView)
        }

        override fun createImageView(container: ViewGroup, position: Int): ImageView {
            return LayoutInflater.from(container.context).inflate(
                R.layout.item_moment_detail_image,
                container,
                false
            ) as ImageView
        }
    }
}
